const users = wx.cloud.database().collection("users")
var util=require('../../utils/util'); 
var app=getApp()
Page({
  data: {
    animationData:{},
  },
  fanhui:function(){
    wx.navigateBack({})
  },
  zhuan:function(){
    var that=this
    var a10=0.05
    var a5=0.1
    var a3=0.15
    var a1=0.7
    var rad=0
    var suiji=parseInt(Math.random()*(99))+1
    var suiji2=parseInt(Math.random()*100)
    var i=0
    if (suiji2<=50) suiji2=0
    else suiji2=1
    var suiji3=parseInt(Math.random()*35)+5
    if (suiji<=(a10*100)) {rad=45*(3+4*suiji2)+suiji3;i=10}
    else if (suiji<=(a10*100+a5*100)) {rad=45*(2+4*suiji2)+suiji3;i=5}
    else if (suiji<=(a10*100+a5*100+a3*100)) {rad=45*(4*suiji2)+suiji3;i=3}
    else if (suiji<=(a10*100+a5*100+a3*100+a1*100)) {rad=45*(1+4*suiji2)+suiji3;i=1}
    let animation = wx.createAnimation({
      duration: 20000,
      timingFunction:'ease',
      transformOrigin:'50% 76% 0'
    })
    animation.rotate(360*50+rad).step()
    that.setData({
      animationData: animation.export()
    })
    users.where({
      _openid: app.globalData.openid
    }).get({
      success(res) {
        var _id = res.data[0]._id
        var tansuo = res.data[0].tansuo
        tansuo+=i
          users.doc(_id).update({
            data: {
              tansuo: tansuo
            },
          })
          setTimeout(function(){
            wx.showModal({
              title: '恭喜',
              content: '抽到'+String(i)+'碳索币，共有'+String(tansuo)+'碳索币',
              showCancel:false,
              confirmText:'确定',
              success (res) {
                if (res.confirm) {
                  wx.navigateBack({})
                }
              }
            })
          },21000)
      }
    })  
  },
  can:function(){
    var that=this
    users.where({
      _openid: app.globalData.openid
    }).get({
      success(res) {
        var timeof = util.formatTime(new Date)
        if (res.data[0].tansuotime.indexOf(timeof) === -1) {
          that.zhuan()
          var tansuotime = res.data[0].tansuotime
          var _id = res.data[0]._id
          tansuotime.push(timeof)
          users.doc(_id).update({
            data: {
              tansuotime: tansuotime
            },
          })
        }
        else{
          wx.showModal({
            title: '提示',
            content: '今日已经进行过大转盘了哦，明天再来吧~',
            showCancel:false,
            confirmText:'确定',
          })
        }
      }
    })
  },
})